<?php

	// +----------------------------------------------------------------------
	// | 公开控制器
	// +----------------------------------------------------------------------
	// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
	// +----------------------------------------------------------------------
	// | Author: 微尘 <yicmf@qq.com>
	// +----------------------------------------------------------------------

	namespace app\controller\admin;

	use app\model\admin\Version as VersionModel;
	use think\facade\Env;

	class Foreign extends Common
	{

		/**
		 * 清理缓存.
		 */
		public function clear()
		{
			if ($this->request->isPost()) {
				$clear = $this->request->post('clear_checked/a');
				if ($clear && is_array($clear) && count($clear) > 0) {
					foreach ($clear as $dir) {
						if (is_file(Env::get('runtime_path') . $dir)) {
							// 如果是文件直接删除
							unlink(Env::get('runtime_path') . $dir);
							$data['status'] = 0;
							$data['msg'] = '清理成功';
						} elseif (is_dir(Env::get('runtime_path') . $dir)) {
							// 文件夹
							foreach (scandir(Env::get('runtime_path') . $dir) as $file) {
								if (is_file(Env::get('runtime_path') . $file)) {
									unlink(Env::get('runtime_path') . $file);
								} else {
									array_map('unlink', glob(Env::get('runtime_path') . $file . '/*.*'));
								}
							}
							$data['status'] = 0;
							$data['msg'] = '清理成功';
						} else {
							$data['status'] = 1;
							$data['msg'] = '位置文件类型';
						}
					}
				} else {
					$data['status'] = 10001;
					$data['msg'] = '未发现需要清理文件';
				}
				return json($data);
			} else {
				$files = array_map('basename', glob(Env::get('runtime_path') . '*'));
				$defualt = $this->clearInfo();
				$lists = [];
				foreach ($files as $file) {
					if (isset($defualt[$file])) {
						$lists[$file] = $defualt[$file];
					} else {
						$lists[$file] = ['info' => '未知文件，请谨慎处理', 'title' => $file, 'clear' => 0, 'color' => 'red'];
					}
				}
				$this->assign('lists', $lists);
				$this->assign('runtime_path', Env::get('runtime_path'));
				return '规划中';
			}
		}

		protected function clearInfo()
		{
			$info['temp'] = ['info' => '临时文件，修改模版文件的时候需要清除', 'title' => '临时文件', 'clear' => 1, 'color' => 'green'];
			$info['log'] = ['info' => '网站运行过程中会记录各种错误日志，以文件的方式保存', 'title' => '日志文件', 'clear' => 0, 'color' => 'blue'];
			$info['schema'] = ['info' => '字段缓存文件', 'title' => '字段缓存文件', 'clear' => 0, 'color' => 'blue'];
			$info['cache'] = ['info' => '缓存文件，可以直接清理', 'title' => '缓存文件', 'clear' => 1, 'color' => 'green'];
			$info['classmap.php'] = ['info' => '优化代码运行速度，在代码改动的情况下需要清理', 'title' => '类库映射', 'clear' => 0, 'color' => 'blue'];
			$info['route.php'] = ['info' => '路由缓存，一般是优化网站运行速度', 'title' => '路由缓存', 'clear' => 0, 'color' => 'blue'];
			return $info;
		}

		public function theme()
		{
			return $this->fetch();
		}

		public function about()
		{
			$this->assign('versions',VersionModel::where('status',1)->order('create_time DESC')->select());
			return $this->fetch();
		}

		public function more()
		{
			return $this->fetch();
		}
	}
